package com.lightsaber.trade.as.web.programme;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.as.entity.programme.Programme;
import com.lightsaber.trade.as.service.param.ParamService;
import com.lightsaber.trade.as.service.programme.ProgrammeManager;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/programme")
@Results({ @Result(name = "info", location = "programme-info.action", type = "redirectAction", params = {
        "programmeId", "${programmeId}" }) })
public class ProgrammeAction extends BaseAction {
    final static Logger log = LoggerFactory.getLogger(ProgrammeAction.class);
    // ****************** Autowired *****************START
    @Autowired
    private transient ProgrammeManager programmeManager;

    @Autowired
    private transient ParamService paramService;

    // ****************** Autowired *****************END

    // ****************** Page parameters *****************START
    private boolean search;

    private Programme programme = new Programme();// As same as ActionForm

    @Value(value = "#{appProperties['programme.page.size']}")
    private String PAGE_SIZE;

    private Page<Programme> page;
    private String copyId;
    private String copyOrRevise;

    private String programmeId;
    // ****************** Page parameters *****************END

    private static final long serialVersionUID = -6133932528005700711L;

    @Override
    public String execute() throws Exception {
        return list();
    }

    public String list() {
        if (search) {
            if (page == null) {
                page = new Page<Programme>(Integer.valueOf(PAGE_SIZE));
            } else {
                page.setPageSize(Integer.valueOf(PAGE_SIZE));
            }

            if (!page.isOrderBySetted()) {
                page.setOrderBy(BaseEntity.ID);
                page.setOrder(Page.ASC);
            }

            programmeManager.findPage(programme, page);
        } else {
            programme = new Programme();
        }

        return SUCCESS;
    }

    public void ajaxValidateUnique() {
        long cnt = programmeManager.countByCodeAndYearAndVersion(programme);

        if (cnt > 0) {
            Struts2Utils.renderText("false");
        } else {
            Struts2Utils.renderText("true");
        }

    }

    // ******************** Bean *******************START
    public void setSearch(final boolean search) {
        this.search = search;
    }

    public boolean getSearch() {
        return search;
    }

    public Programme getProgramme() {
        return programme;
    }

    public Page<Programme> getPage() {
        return page;
    }

    public void setPage(final Page<Programme> page) {
        this.page = page;
    }

    public ProgrammeManager getProgrammeManager() {
        return programmeManager;
    }

    public void setProgrammeManager(final ProgrammeManager programmeManager) {
        this.programmeManager = programmeManager;
    }

    public ParamService getParamService() {
        return paramService;
    }

    public List<Programme> getFeederList() {
        return programmeManager.getFeederList();
    }

    public String getCopyId() {
        return copyId;
    }

    public void setCopyId(final String copyId) {
        this.copyId = copyId;
    }

    public String getCopyOrRevise() {
        return copyOrRevise;
    }

    public void setCopyOrRevise(final String copyOrRevise) {
        this.copyOrRevise = copyOrRevise;
    }

    public String getProgrammeId() {
        return programmeId;
    }

    public void setProgrammeId(String programmeId) {
        this.programmeId = programmeId;
    }

    // ******************** Bean *******************END
}
